package com.example.klaraservice.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.example.klaraservice.vo.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author : Glacialerisa
 * @date : 2023/6/6 22:22
 * @desc :
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/api/files")
public class UploadFileController {
    @Value("${server.port}")
    private String port;
//    private static final String IP = "http://localhost";
    private static final String IP = "http://172.17.33.23";
    /**
     * 上传接口
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();//获取源文件的名称
//        定义文件的唯一标识（前缀）
        String flag = IdUtil.fastSimpleUUID();
        //商品+c、用户+u、品牌+b
        String rootFilePath = System.getProperty("user.dir")+"/src/main/resources/images/"+flag+"_"+originalFilename;//获取文件上传的路径
        try {
            FileUtil.writeBytes(file.getBytes(),rootFilePath);//把文件写入该路径
            System.out.println(rootFilePath+"-------");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = IP+":"+port+"/api/files/download/"+flag;
        return R.ok().data("url", url);//返回结果url
    }
    /**
     * 下载接口
     * @param flag
     * @param response
     */
    @GetMapping("/download/{flag}")
    public void getFiles(@PathVariable String flag, HttpServletResponse response){
        OutputStream os;//新建一个输出对象
        String basePath = System.getProperty("user.dir")+"/src/main/resources/images/";//文件路径
        List<String> fileNames = FileUtil.listFileNames((basePath));//获取所有的文件名称
        String fileName = fileNames.stream().filter(name -> name.contains(flag)).findAny().orElse("");//找到根参数一致的文件

        try {
            if (StrUtil.isNotEmpty(fileName)){
                response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
                response.setContentType("application/octet-stream");
                byte[] bytes = FileUtil.readBytes(basePath + fileName);//通过文件路径读取文字节流
                os = response.getOutputStream();//通过输出流返回文件
                os.write(bytes);
                os.flush();
                os.close();
            }
        }catch (Exception e){
            System.out.println("文件下载失败");
        }
    }
}
